package com.example.consumer.controller;

import com.example.consumer.common.CommonResponse;
import com.example.consumer.entity.Db;
import com.example.consumer.entity.DbKey;
import com.example.consumer.mapper.DbMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

@RestController
@RequestMapping("/user")
public class HelloController {

    @Autowired
    private DbMapper mapper;

    @Value("${server.port}")
    private String port;

    @GetMapping("/hello")
    public List<Db> hello() {
        DbKey key = new DbKey();
        key.setDb("sys");
        List<Db> all = mapper.findAll();
        return all;
    }

    @GetMapping("/queryDb")
    public List<Db> queryDb() {
        List<Db> list = new ArrayList<>();
        Db db = new Db();
        db.setAlterPriv("alterpriv");
        db.setCreatePriv("dali");
        db.setDeletePriv("deletePriv");
        Collections.addAll(list,db);
        return list;
    }

    @PostMapping("/addDb")
    public CommonResponse addDb(@RequestBody Db db) {
        CommonResponse response = new CommonResponse();
        int insert = mapper.insert(db);
        response.setData(insert);
        response.setMessage("add success");
        return response;
    }
}
